<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
          v-hasPermi="['production:exceedReason:edit']"
        >
          修改
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="exportExcel"
        >
          导出当前数据
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 0px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <Tag type="dot" size="default" color="#09aa46">启动</Tag>
      <Tag type="dot" size="default" color="#bb0331">等待</Tag>
      <Tag type="dot" size="default" color="#4169E1">暂停</Tag>
      <Tag type="dot" size="default" color="#FAFFF0">完成</Tag>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="exceedReasonList"
      border
      id="test"
      stripe
      :cell-style="iCellStyle"
      highlight-current-row
      :height="tableH"
      @row-click="clickRow"
      :header-cell-style="{ 'text-align': 'center', padding: '0px' }"
    >
      <el-table-column
        label="#"
        type="index"
        fixed="left"
        align="center"
        width="50"
      />
      <el-table-column
        label="制造单号"
        align="center"
        prop="moeZzdh"
        width="135"
      />
      <el-table-column
        label="客户编码"
        align="center"
        width="135"
        prop="itmCustwldm"
      />
      <el-table-column
        label="订单计划数量"
        width="100"
        align="center"
        prop="moeScsl"
      />
      <el-table-column
        label="良品数量"
        align="center"
        prop="moeLpsl"
        width="100"
      />
      <el-table-column
        label="已入库数"
        show-overflow-tooltip
        prop="moeRksl"
        align="center"
        width="100"
      />
      <el-table-column
        label="生产物料代码"
        align="center"
        prop="moeWldm"
        width="135"
      />
      <el-table-column
        label="生产物料品名规格"
        show-overflow-tooltip
        prop="itmPmgg"
        width="400"
      />
      <el-table-column
        label="入库物料"
        align="center"
        prop="moeRkwl"
        width="135"
      />
      <el-table-column
        label="入库仓库"
        align="center"
        prop="moeCkdm"
        width="135"
      >
        <template #default="scope">
          <dict-tag :options="allStorageOptions" :value="scope.row.moeCkdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="入库物料品名规格"
        show-overflow-tooltip
        prop="moeRkpmgg"
        width="400"
      />
      <el-table-column
        label="销售单号"
        align="center"
        prop="moeSodh"
        width="135"
      />
      <el-table-column
        label="模具编号"
        show-overflow-tooltip
        prop="moeMjbh"
        width="200"
      />
      <el-table-column label="机台" align="center" prop="moeJtbh" width="100" />
      <el-table-column
        label="生产日期"
        align="center"
        prop="moeScrq"
        width="150"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.moeScrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 修改采购属性对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="900px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <!--        123-->
        <el-col :span="12">
          <el-form-item label="制造单号" label-width="110px" prop="moeZzdh">
            <el-input v-model="form.moeZzdh" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销售单号" label-width="110px" prop="moeSodh">
            <el-input v-model="form.moeSodh" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料代码" label-width="110px" prop="moeWldm">
            <el-input v-model="form.moeWldm" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="品名规格" label-width="110px" prop="moeRkpmgg">
            <el-input v-model="form.moeRkpmgg" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划数量" label-width="110px" prop="moeScsl">
            <el-input v-model="form.moeScsl" disabled />
          </el-form-item>
        </el-col>
        <!--        123-->
        <el-col :span="12">
          <el-form-item label="模具编号" label-width="110px" prop="moeMjbh">
            <el-input v-model="form.moeMjbh" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="良品数量" label-width="110px" prop="moeLpsl">
            <el-input v-model="form.moeLpsl" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="已入库数" label-width="110px" prop="moeRksl">
            <el-input v-model="form.moeRksl" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入库仓库" label-width="110px" prop="moeCkdm">
            <el-select
              v-model="form.moeCkdm"
              style="width: 320px"
              disabled
              clearable
              placeholder="请选择入库仓库"
            >
              <el-option
                v-for="item in allStorageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入库物料">
            <el-select
              v-model="form.moeRkwl"
              filterable
              style="width: 320px"
              @change="changeWldm"
              clearable
            >
              <el-option
                v-for="item in reasonCategoryOptions"
                :key="item.molWldm"
                :label="item.itmPmgg"
                :value="item.molWldm"
              >
                <!--                :value="{value:dict.lbmId,label:dict.lbmLbdm}"-->
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <!--    查询对话框-->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="720px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="88px"
      >
        <el-col :span="12">
          <el-form-item label="完工日期">
            <el-date-picker
              v-model="daterangeSdm"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="制造单号" prop="moeZzdh">
            <el-input
              v-model="queryParams.moeZzdh"
              placeholder="请输入制造单号"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销售单号" prop="moeSodh">
            <el-input
              v-model="queryParams.moeSodh"
              placeholder="请输入销售单号"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料代码" prop="moeWldm">
            <el-input
              v-model="queryParams.moeWldm"
              placeholder="请输入物料代码"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具编号" prop="moeMjbh">
            <el-input
              v-model="queryParams.moeMjbh"
              placeholder="请输入模具编号"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="机台编号" prop="moeJtbh">
            <el-input
              v-model="queryParams.moeJtbh"
              placeholder="请输入机台编号"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="品名规格" prop="itmPmgg">
            <el-input
              v-model="queryParams.itmPmgg"
              placeholder="请输入品名规格"
              clearable
              style="width: 580px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-checkbox style="margin-left: 300px" v-model="queryParams.flag"
            >只显示未确认</el-checkbox
          >
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="openSearch = false">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { exportTable } from "@/utils/ruoyi";
import { editStopReason, listStopReason } from "@/api/production/stopReason";
import { getReasonDescribleByCategory } from "@/api/system/dict/data";
import {
  editExceedReason,
  listStopExceedReason,
} from "@/api/production/exceedReason";
import {
  exportInputConfirm,
  listInputConfirm,
  moeModify,
  wldmSelect,
} from "@/api/production/inputConfirm";
import { getAll, getstorageType } from "@/api/repertory/Storage";

export default {
  name: "InputConfirm",
  data() {
    return {
      // 遮罩层
      loading: true,
      checkRk: false,
      // 导出遮罩层
      exportLoading: false,
      tableH: document.body.clientHeight - 230,
      show: true,
      openSearch: false,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购属性表格数据
      exceedReasonList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      nowData: null,
      disabled: true,
      // 原因类别
      reasonCategoryOptions: [],
      allStorageOptions: [],
      // 原因描述
      reasonDescribeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 40,
        moeJtbh: null,
        moeMjbh: null,
        itmPmgg: null,
        moeWldm: null,
        moeSodh: null,
        moeZzdh: null,
        flag: true,
      },
      // 建立日期时间范围
      daterangeSdm: [
        this.$moment().startOf("month").toDate(),
        this.$moment().add(100, "year").endOf("month").toDate(),
      ],
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ztlLbdm: [
          { required: true, message: "原因类别不能为空", trigger: "blur" },
        ],
        ztlYyms: [
          { required: true, message: "原因描述不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    getAll().then((response) => {
      this.allStorageOptions = response.data;
      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    this.getList();
  },
  methods: {
    iCellStyle({ row, column, rowIndex, columnIndex }) {
      //启动
      if (row.moeZtbz == "1") {
        return "color:#09aa46";
      } else if (row.moeZtbz == "2") {
        return "color:#bb0331";
      } else if (row.moeZtbz == "3") {
        return "color:#4169E1";
      } else if (row.moeZtbz == "4 ") {
        return "color:#FAFFF0";
      }
    },
    /** 查询采购属性列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeSdm && "" != this.daterangeSdm) {
        this.queryParams.params["beginMoeScrq"] = this.moment(
          this.daterangeSdm[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endMoeScrq"] = this.moment(
          this.daterangeSdm[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      listInputConfirm(this.queryParams).then((response) => {
        console.log(response);
        this.exceedReasonList = response.rows;
        this.total = response.total;
        this.loading = false;
        if (this.exceedReasonList.length > 0) {
          this.nowData = this.exceedReasonList[0];
          this.show = false;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        //123
        moeZzdh: null,
        moeSodh: null,
        moeWldm: null,
        moeRkpmgg: null,
        moeScsl: null,
        moeMjbh: null,
        moeLpsl: null,
        moeRksl: null,
        moeCkdm: null,
        moeRkwl: null,
        moeId: null,
      };
      this.resetForm("form");
    },

    clickRow(row) {
      if (row) {
        this.nowData = row;
      }
    },
    hgandleCheck(value) {
      if (value) {
        this.queryParams.flag = 0;
      } else {
        this.queryParams.flag = 1;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleSearch() {
      this.reset();
      this.title = "查询";
      this.openSearch = true;
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      //123
      this.form.moeZzdh = this.nowData.moeZzdh;
      this.form.moeSodh = this.nowData.moeSodh;
      this.form.moeWldm = this.nowData.moeWldm;
      this.form.moeRkpmgg = this.nowData.itmPmgg;
      this.form.moeScsl = this.nowData.moeScsl;
      this.form.moeMjbh = this.nowData.moeMjbh;
      this.form.moeLpsl = this.nowData.moeLpsl;
      this.form.moeRksl = this.nowData.moeRksl;
      this.form.moeCkdm = this.nowData.moeCkdm;

      this.form.moeId = this.nowData.moeId;
      wldmSelect(this.nowData.moeId)
        .then((response) => {
          this.reasonCategoryOptions = response.data;
          this.form.moeRkwl = this.nowData.moeRkwl;
        })
        .then((res) => {
          for (let i of this.reasonCategoryOptions) {
            if (i.molWldm === this.nowData.moeRkwl) {
              this.form.moeCkdm = i.itmCkdm;
              break;
            }
          }
        });

      this.open = true;
      this.title = "修改";
    },
    exportExcel() {
      const queryParams = this.queryParams;
      this.$confirm("确认要导出所有生产入库数据吗？", "确认", {
        type: "info",
        cancelButtonText: "取消",
        confirmButtonText: "确认",
      })
        .then(() => {
          this.exportLoading = true;
          return exportInputConfirm(queryParams);
        })
        .then((res) => {
          this.download(res.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    changeWldm(data) {
      if (data) {
        for (let i of this.reasonCategoryOptions) {
          if (i.molWldm === data) {
            this.form.moeCkdm = i.itmCkdm;
            break;
          }
        }
      } else {
        this.form.moeCkdm = null;
      }
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          moeModify(this.form).then((res) => {
            this.open = false;
            this.msgSuccess("确认成功");
            this.getList();
          });
        }
      });
    },
  },
};
</script>
